<?php

/**
 * TODO:分身...
 * auction.js  v
 */
session_start();
define('ROOT_PATH', str_replace("\\", '/', dirname(__FILE__)));
$config = include(ROOT_PATH.'/Lib/common.ini.php');

	$data = Init_GP(array('id'));
	$id = (int)($data['id']);
	$userdata = $_SESSION['user'];
	$sql = "SELECT * FROM `{$prefix}user` where `id`={$userdata['id']}";
	$userdata = $db->fetch_first($sql);
	unset($sql);
	$auctiondata = $get->getAuctionData($id);
	if (empty($auctiondata)){
		$msg = 0; //竞拍不存在
	}else {
		$sql = "SELECT * FROM `{$prefix}daemon` WHERE `aid` = {$auctiondata['id']}  and `uid` = {$userdata['id']} and `type` = 0 and `state` = 0";
		$daemondata = $db->fetch_first($sql);//读出来
		unset($sql);
		if(empty($daemondata)){
		   $msg = 0; //守秒不存在
		}else{
			$time = time();
			$snum=$auctiondata['endtime']-$time;
			if($snum>=1){
			   $msg = 1; //守秒已存在
			}else{
				if($auctiondata['type'] == 1 && $userdata['old'] == 1){
					$msg = 0;//新人竞拍
				}else {
					$bool = true;
					if(!empty($auctiondata['boutlimit'])){
						$sql = "SELECT count(*) as count FROM `{$prefix}auction_log` where `aid` = {$auctiondata['id']} and `uid` = {$userdata['id']}";
						$bout = $db->fetch_first($sql);
						$bout = $bout['count'];
						if($bout >= $auctiondata['boutlimit']){
							$bool = false;
						}
					}
					if($bool){
						$sql = "SELECT * FROM `{$prefix}participate` where `aid`={$auctiondata['id']} and `uid`={$userdata['id']}";
						$participate = $db->fetch_first($sql);
						if (empty($participate)){
							$sql = "INSERT INTO `{$prefix}participate` (`id`, `uid`, `aid`) VALUES (NULL, '{$userdata['id']}', '{$auctiondata['id']}')";
							$db->query($sql);
						}			
						if ($auctiondata['uid'] == $userdata['id']){
							$msg = 0; //最后竞拍者是自己
							setdaemon($userdata,$auctiondata);
						}else {
							if (!empty($auctiondata['isdiscount'])){
								if(!empty($auctiondata['beforebout']) && !empty($auctiondata['discount'])){
									if($auctiondata['auctioncount'] <= $auctiondata['beforebout']){
										$auctiondata['usebeat'] = $auctiondata['usebeat'] * $auctiondata['discount'] / 10;
									}
								}
							}
														
							if ($userdata['priority'] == 1){  //优先使用闪点
								if ($userdata['beat'] < $auctiondata['usebeat']){
									if ($userdata['pointb'] < $auctiondata['usebeat']){
										$msg = 0; //余额不足
									}else {
										//执行拍点竞拍操作
										usepointb($userdata,$auctiondata);
										addpointc($userdata,$auctiondata);
										setdaemon($userdata,$auctiondata);
										$msg = 0;
									}	
								}else {
									//执行闪点竞拍操作
									usebeat($userdata,$auctiondata);
									addpointc($userdata,$auctiondata);
									setdaemon($userdata,$auctiondata);
									$msg = 0;
								}
							}elseif ($userdata['priority'] == 2){ //优先使用拍点
								if ($userdata['pointb'] < $auctiondata['usebeat']){
									if ($userdata['beat'] < $auctiondata['usebeat']){
										$msg = 0; //余额不足
									}else {
										//执行闪点竞拍操作
										usebeat($userdata,$auctiondata);
										addpointc($userdata,$auctiondata);
										setdaemon($userdata,$auctiondata);
										$msg = 0;
									}	
								}else {
									//执行拍点竞拍操作
									usepointb($userdata,$auctiondata);
									addpointc($userdata,$auctiondata);
									setdaemon($userdata,$auctiondata);
									$msg = 0;
								}
							}
						}
					}else{
						$msg = 0;
					}
				}
			}
		}
	}
    echo $msg;	
	
	//使用闪点
	function usebeat($userdata,$auctiondata){
		//编辑用户点数
		global $db,$prefix,$cache,$get;
		
		$sql = "UPDATE `{$prefix}user` SET `beat` = `beat` - {$auctiondata['usebeat']} WHERE `id` = {$userdata['id']}";
		$db->query($sql);
		
		$currentprice = $auctiondata['realprice'] + $auctiondata['addprice']; //当前价格
		
		unset($sql);
		$ip = get_client_ip();
		$address = getlocaltion($ip);
		$time = time();
		$sql = "INSERT INTO `{$prefix}auction_log` (`id`, `aid`, `uid`, `name`, `goodsname`, `beat`, `pointb`, `ip`, `address`, `bidprice`, `addtime`) 
				VALUES (NULL, '{$auctiondata['id']}', '{$userdata['id']}', '{$userdata['name']}', '{$auctiondata['goodsname']}', '{$auctiondata['usebeat']}'
				,'0', '{$ip}', '{$address}', '{$currentprice}', '{$time}')";
		$logdata = $get->getLogData($auctiondata['id']); //先获取缓存
		
		$db->query($sql); //再写进数据库
		
		$log_id = $db->insert_id();
		
		unset($sql);
		$sql = "SELECT * FROM `{$prefix}auction_log` WHERE `id` = {$log_id}";
		$tmp = $db->fetch_first($sql);
		
		$num = array_unshift($logdata,$tmp);
		if ($num > 10){
			array_pop($logdata);
		}
		$key = 'history_'.$auctiondata['id'];
		$cache->set($key,$logdata);
		
		//对竞拍内容的更新
		if($auctiondata['maxaddsecond']>0){
			$diftime = $auctiondata['endtime']-$time;
			if($diftime < $auctiondata['maxaddsecond']){
				$currenttime = $auctiondata['endtime'] + $auctiondata['addsecond']; //当前时间
				if($currenttime-$time > $auctiondata['maxaddsecond'])$currenttime=$time+$auctiondata['maxaddsecond'];
			}else{
				$currenttime = $auctiondata['endtime'];
			}
		}else{
			$currenttime = $auctiondata['endtime'] + $auctiondata['addsecond']; //当前时间
		}
		
		$auctioncount = $auctiondata['auctioncount']+1;   //对数据库的竞拍数据更新
		unset($sql);
		$sql = "UPDATE `{$prefix}auction` SET `uid` = '{$userdata['id']}', `auctionuname` = '{$userdata['name']}', 
				`auctioncount` = '{$auctioncount}', `realprice` = '{$currentprice}', `endtime` = '{$currenttime}' 
				WHERE `id` = {$auctiondata['id']}";
		$db->query($sql); //写进数据库
		
		//更新缓存中的内容
		$sql = "SELECT * FROM `{$prefix}auction` where `id`={$auctiondata['id']}";
		$cachedata = $db->fetch_first($sql);
		$cachedata['head'] = $userdata['head'];
		$cachedata['uptime'] = $time;
		
		$cache->set($auctiondata['id'],$cachedata);
	}
	
	//使用闪点
	function usepointb($userdata,$auctiondata){
		//编辑用户点数
		global $db,$prefix,$cache,$get;
		
		$sql = "UPDATE `{$prefix}user` SET `pointb` = `pointb` - {$auctiondata['usebeat']} WHERE `id` = {$userdata['id']}";
		$db->query($sql);
		
		$currentprice = $auctiondata['realprice'] + $auctiondata['addprice']; //当前价格
		
		unset($sql);
		$ip = get_client_ip();
		$address = getlocaltion($ip);
		$time = time();
		$sql = "INSERT INTO `{$prefix}auction_log` (`id`, `aid`, `uid`, `name`, `goodsname`, `beat`, `pointb`, `ip`, `address`, `bidprice`, `addtime`) 
				VALUES (NULL, '{$auctiondata['id']}', '{$userdata['id']}', '{$userdata['name']}', '{$auctiondata['goodsname']}', '0'
				,'{$auctiondata['usebeat']}', '{$ip}', '{$address}', '{$currentprice}', '{$time}')";
		$logdata = $get->getLogData($auctiondata['id']); //先获取缓存
		$db->query($sql); //再写进数据库
		
		$log_id = $db->insert_id(); //获取刚才写入的ID
		
		unset($sql);
		$sql = "SELECT * FROM `{$prefix}auction_log` WHERE `id` = {$log_id}";
		$tmp = $db->fetch_first($sql);//再把它读出来
		
		$num = array_unshift($logdata,$tmp);     //然后对 缓存内容进行更新
		if ($num > 10){
			array_pop($logdata);
		}
		$key = 'history_'.$auctiondata['id'];
		$cache->set($key,$logdata);
		
		//对竞拍内容的更新
		if($auctiondata['maxaddsecond']>0){
			$diftime = $auctiondata['endtime']-$time;
			if($diftime < $auctiondata['maxaddsecond']){
				$currenttime = $auctiondata['endtime'] + $auctiondata['addsecond']; //当前时间
				if($currenttime-$time > $auctiondata['maxaddsecond'])$currenttime=$time+$auctiondata['maxaddsecond'];
			}else{
				$currenttime = $auctiondata['endtime'];
			}
		}else{
			$currenttime = $auctiondata['endtime'] + $auctiondata['addsecond']; //当前时间
		}
		
		$auctioncount = $auctiondata['auctioncount']+1;   //对数据库的竞拍数据更新
		unset($sql);
		$sql = "UPDATE `{$prefix}auction` SET `uid` = '{$userdata['id']}', `auctionuname` = '{$userdata['name']}', 
				`auctioncount` = '{$auctioncount}', `realprice` = '{$currentprice}', `endtime` = '{$currenttime}' 
				WHERE `id` = {$auctiondata['id']}";
		$db->query($sql); //写进数据库
		
		//更新缓存中的内容
		$sql = "SELECT * FROM `{$prefix}auction` where `id`={$auctiondata['id']}";
		$cachedata = $db->fetch_first($sql);
		$cachedata['head'] = $userdata['head'];
		$cachedata['uptime'] = $time;
		
		$cache->set($auctiondata['id'],$cachedata);
		
	}
    //赠送积分
	function addpointc($userdata,$auctiondata){
		//编辑用户积分
		if($auctiondata['pointc']>0){
			global $db,$prefix,$sysconfig;
			
			$sql = "UPDATE `{$prefix}user` SET `pointc` = `pointc` + {$auctiondata['pointc']} WHERE `id` = {$userdata['id']}";
			$db->query($sql);//赠送积分写进数据库
			unset($sql);
			
			$payment="参加竞拍";
			$time = time();
			$remark="参加竞拍,{$auctiondata['goodsname']},点击1次,赠送{$auctiondata['pointc']}{$sysconfig['pointc_name']}";
			$sql = "INSERT INTO `{$prefix}pointclog` (`id`, `uid`, `name`, `payment`, `money`, `addtime`, `remark`) 
					VALUES (NULL, '{$userdata['id']}', '{$userdata['name']}', '{$payment}', '{$auctiondata['pointc']}', '{$time}','{$remark}')";
			$db->query($sql); //日志再写进数据库
			unset($sql);
		}
	}
	//更改守秒状态
	function setdaemon($userdata,$auctiondata){
		global $db,$prefix;
		
		$sql = "UPDATE `{$prefix}daemon` SET `state` = 1 WHERE `aid` = {$auctiondata['id']} and `uid` = {$userdata['id']} and `type` = 0 and `state` = 0";
		$db->query($sql);//赠送积分写进数据库
		unset($sql);
	}
?>